/**
 * ...
 * @author Krzysztof Rozalski
 */

package hapi.util.benchmark;
import haxe.FastList;

class BenchmarkRunner {

	var benchmarks:FastList<Benchmark>;
	var callOnFinish:Void->Void;
	var iterations:Int;
	
	public static inline var DELAY_BETWEEN_ITERATONS = 800;
	public static inline var DELAY_BEFORE_FIRST_RUN = 1500;
	
	public function new() {
		benchmarks = new FastList<Benchmark>();
	}
	
	public function runWhenReady(?times=1, ?callOnFinish:Void->Void) {
		this.callOnFinish = callOnFinish;
		this.iterations = times;
		haxe.Timer.delay(runBenchmarks, DELAY_BEFORE_FIRST_RUN);
	}
	
	public function add(b:Benchmark):Void {
		benchmarks.add(b);
	}
	
	function runBenchmarks() {
		--iterations;
		for ( benchmark in benchmarks ) {
			benchmark.run();
		}
		if(iterations<=0){
			if (callOnFinish != null) callOnFinish();
		}else {
			haxe.Timer.delay(runBenchmarks, DELAY_BETWEEN_ITERATONS);
		}
	}
}